package com.common.aop;


import com.common.aop.utils.UserAgentTools;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by sola on 2018/1/21.
 */

@Aspect
@Component
@Slf4j
public class DeviceAdapter {
    //private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String MOBILE_PREFIX = "mobile/";

    private static final String USER_AGENT = "User-Agent";

    /**
     * 切入点：com 下所有 @GetMapping 方法
     */
    @Pointcut("execution(* com.*.controller..*(..)) && @annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void controllerMethodPointcut() {
    }

    /**
     * 识别用户请求的设备并返回对应的页面
     */
    @Around("controllerMethodPointcut()")
    public String around(ProceedingJoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            try {
                HttpServletRequest request = attributes.getRequest();
                String userAgent = request.getHeader(USER_AGENT);
                log.info(userAgent);
                Integer deviceType = UserAgentTools.recognize(userAgent);
                String path = (String) joinPoint.proceed();
                return deviceType == UserAgentTypeEnum.PHONE.getCode() ?  MOBILE_PREFIX + path : path;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        throw new RuntimeException("DeviceAdapter，ServletRequestAttributes is null!");
    }

}
